<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="已检订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入已检订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物类型名" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="货物类型名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-input
          v-model="queryParams.orderType"
          placeholder="订单类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="inspectionStatus">
        <el-select v-model="queryParams.inspectionStatus" placeholder="质检员状态" clearable>
          <el-option
            v-for="dict in dict.type.lg_quality_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- 创建时间范围选择 -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['quality:outQuality:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--   排序号   -->
      <el-table-column label="序号" align="center" prop="id" width="70">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>

      <el-table-column label="id" align="center" prop="id" width="70"/>
      <el-table-column label="已检订单编号" align="center" prop="orderNumber" />
      <el-table-column label="订单类型" align="center" prop="orderType" width="100" />
      <el-table-column label="已检货物类型名" align="center" prop="goodsName" />
      <el-table-column label="卖家名" align="center" prop="sellerName" />

      <el-table-column label="状态" align="center" prop="inspectionStatus" width="70">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_quality_status" :value="scope.row.inspectionStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="质检员" align="center" prop="qualityName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['quality:outQuality:xiangqing']"
          >查看详情</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="已检订单编号" prop="orderNumber">
          <el-input v-model="form.orderNumber" disabled />
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-input v-model="form.orderType" disabled />
        </el-form-item>
        <el-form-item label="质检员姓名" prop="qualityName">
          <el-input v-model="form.qualityName" disabled />
        </el-form-item>
        <el-form-item label="卖家名" prop="sellerName">
          <el-input v-model="form.sellerName" disabled />
        </el-form-item>
        <el-form-item label="质检照片" prop="images">
          <image-upload v-model="form.images" disabled/>
        </el-form-item>
        <el-form-item label="二维码照片" prop="qrImage">
          <image-upload v-model="form.qrImage" disabled/>
        </el-form-item>
<!--        <el-form-item label="质检员状态" prop="inspectionStatus">-->
<!--          <el-radio-group v-model="form.inspectionStatus" disabled>-->
<!--            <el-radio-->
<!--              v-for="dict in dict.type.lg_quality_status"-->
<!--              :key="dict.value"-->
<!--              :label="dict.value"-->
<!--            >{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listOutQuality, getOutQuality,   updateOutQuality } from "@/api/quality/outQuality";

export default {
  name: "Post",
  dicts: ['lg_quality_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dateRange: [],
      // 质检员表格数据
      postList: [],
      // 弹出层标题
      title: "123",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: undefined,
        goodsName: undefined,
        orderType: undefined,
        inspectionStatus: undefined,
        startDate: undefined,
        endDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realName: [
          { required: true, message: "质检员名称不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "质检员用户名不能为空", trigger: "blur" }
        ],
        account: [
          { required: true, message: "质检员密码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询质检员列表 */
    getList() {
      this.loading = true;
      listOutQuality(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.postList = response.rows;
        console.log(response);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderNumber: undefined,
        orderType: undefined,
        goodsName: undefined,
        sellerName: undefined,
        qualityName: undefined,
        frontImage: undefined,
        backImage: undefined,
        inspectionStatus: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.dateRange && this.dateRange.length === 2) {
    this.queryParams.startDate = this.dateRange[0];
    this.queryParams.endDate = this.dateRange[1];
  } else {
    this.queryParams.startDate = undefined;
    this.queryParams.endDate = undefined;
  }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
  this.queryParams.startDate = undefined;
  this.queryParams.endDate = undefined;
  this.resetForm("queryForm");
  this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 查看详情按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getOutQuality(id).then(response => {
        console.log("response data:", response.data);
        if (response.data && response.data.length > 0) {
          // 修改这里，从数组中获取第一个元素
          this.form = response.data[0];
          this.open = true;
          this.title = "查看详情";
        } else {
          this.$modal.msgError("获取详情数据失败");
        }
      }).catch(error => {
        console.error("获取详情数据出错:", error);
        this.$modal.msgError("获取详情数据出错");
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateOutQuality(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('quality/outQuality/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
